/***
This do-file creates 3 figures that plot changes in small business revenue vs
ZIP code characteristics: median income, population density, median two-bedroom
rent.
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"
local category "Small Business Revenue"

* Create required subfolders
cap mkdir "${root}/results/Small Business Revenue"
cap mkdir "${root}/results/paper numbers"
cap mkdir "${root}/results/paper numbers/`category'"

* Erase output numbers
cap erase "${root}/results/paper numbers/`category'/Changes in Small Business Revenue vs Median Income (zip).yaml"
cap erase "${root}/results/paper numbers/`category'/Changes in Small Business Revenue vs Population Density (zip).yaml"
cap erase "${root}/results/paper numbers/`category'/Changes in Small Business Revenue vs Median Rent (zip).yaml"

*-------------------------------------------------------------------------------
* Load and process data
*-------------------------------------------------------------------------------

project, uses("${root}/data/web/data/Womply - ZCTA - 2020.csv")
import delimited  "${root}/data/web/data/Womply - ZCTA - 2020.csv", clear

* Convert the changes to percentages
gen revenue_change_100 = revenue_all_apr2020 * 100

* Merge ZCTA-level covariates
project, uses("${root}/data/derived/ACS 2014-2018 5-Year ZCTA/ACS 2014-2018 ZCTA.dta")
merge 1:1 zcta using "${root}/data/derived/ACS 2014-2018 5-Year ZCTA/ACS 2014-2018 ZCTA.dta", keep(1 3) nogen keepusing(pop_2014_2018_est medhhinc_2014_2018_est med_2br_2014_2018_est zarealand)
gen zarealand_sqm = zarealand / (1609.37 ^ 2)
drop zarealand
rename (pop_2014_2018_est medhhinc_2014_2018_est med_2br_2014_2018_est) (pop_2018 medhhinc_2018 med_2br_2018)

* Gen population density by ZCTA
gen pop_conc_zcta = pop_2018 / zarealand_sqm

* Put population density on a log scale
gen l_pop_conc_zcta = log(pop_conc_zcta)

* Merge counties (for SEs)
rename zcta zcta5
preserve
	project, uses("${root}/data/dvc/ACS 2014-2018 5-Year ZCTA/Area/zcta_county_rel_10.txt")
	import delimited "${root}/data/dvc/ACS 2014-2018 5-Year ZCTA/Area/zcta_county_rel_10.txt", encoding(ISO-8859-2) clear
	sort zcta5 poppt
	bysort zcta5 (poppt): keep if _n == _N // only keeps the county where most of the zcta lives
	gen county_fips = 1000*state + county
	keep zcta5 county_fips
	tempfile zcta_county_CW
	save `zcta_county_CW'
restore

merge 1:1 zcta5 using `zcta_county_CW', keep(1 3) nogen
rename zcta5 zcta

*-------------------------------------------------------------------------------
* Plot figures
*-------------------------------------------------------------------------------

foreach var in medhhinc_2018 l_pop_conc_zcta med_2br_2018 {

		reg revenue_change_100 `var' [w = pop_2018], vce(cluster county_fips)
		local beta = _b[`var'] * 1000
		local display_beta: display %4.2f `beta'
		local std_err = _se[`var'] * 1000
		local display_std_err: display %4.2f `std_err'

	if "`var'" == "medhhinc_2018" {
		local x_title "Median Household Income in 2014-2018 ($)"
		local covar "Median Income"
		local xscale "25000 155000"
		local xlabel `"25e3 "$25,000" 50e3 "$50,000" 75e3 "$75,000" 100e3 "$100,000" 125e3 "$125,000" 150e3 "$150,000""'
		local x_text "25000"
		local key "inc"
		local unit "/$1000"
		local yscale "-55 -40"
		local ylabel -55 "-55%" -50 "-50%" -45 "-45%" -40 "-40%"
		local y_text "-55"
	}

	else if "`var'" == "l_pop_conc_zcta" {

		reg revenue_change_100 `var' [w = pop_2018], vce(cluster county_fips)
		local beta = _b[`var']
		local display_beta: display %4.2f `beta'
		local std_err = _se[`var']
		local display_std_err: display %4.2f `std_err'

		local x_title "Population Density: Inhabitants per Square Mile in 2014-2018 (Log Scale)"
		local covar "Population Density"
		local xscale "3.9 10.2"
		local xlabel 3.9 "50" 4.6 "100" 6 "400" 7.4 "1,600" 8.8 "6,400" 10.2 "25,600"
		local x_text "3.9"
		local key "pop"
		local unit ""
		local yscale "-60 -30"
		local ylabel -60 "-60%" -50 "-50%" -40 "-40%" -30 "-30%"
		local y_text "-60"
	}

	else if "`var'" == "med_2br_2018" {
		local x_title "Median Two Bedroom Monthly Rent in 2014-2018 ($)"
		local covar "Median Rent"
		local xscale "500 2250"
		local xlabel `"400 "$400" 1000 "$1,000" 1600 "$1,600" 2200 "$2,200""'
		local x_text "400"
		local key "rent"
		local unit "/$1000"
		local yscale "-63 -27"
		local ylabel -60 "-60%" -50 "-50%" -40 "-40%" -30 "-30%"
		local y_text "-63"
	}

	binscatter revenue_change_100 `var' ///
		[w = pop_2018], ///
		xtitle("`x_title'") ///
		xscale(range(`xscale')) ///
		yscale(range(`yscale')) ///
		xlabel(`xlabel', format(%9.0gc)) ///
		ytitle("Change in Small Business Revenue (%)" "from January to April 2020") ///
		ylabel(`ylabel', nogrid) ///
		text(`y_text' `x_text' "Slope = `display_beta'%`unit' (s.e. = `display_std_err')", place(ne) just(left) color(gs8) size(medlarge)) ///
		legend(off) ///
		${title_`version'}

	oi_graph_export "${root}/results/Small Business Revenue/Small Business Revenue vs `covar' binscatter", type(${fig_type})

	* Black and white version for QJE
	if "`var'" == "med_2br_2018" {
		binscatter revenue_change_100 `var' ///
		[w = pop_2018], mcolor(gs0) lcolor(gs8) ///
		xtitle("`x_title'") ///
		xscale(range(`xscale')) ///
		yscale(range(`yscale')) ///
		xlabel(`xlabel', format(%9.0gc)) ///
		ytitle("Change in Small Business Revenue (%)" "from January to April 2020") ///
		ylabel(`ylabel', nogrid) ///
		text(`y_text' `x_text' "Slope = `display_beta'%`unit' (s.e. = `display_std_err')", place(ne) just(left) color(gs8) size(medlarge)) ///
		legend(off) ///
		${title_`version'}

		graph export "${root}/results/QJE_Figures_BlackAndWhite/Figure_3.svg", replace
		project, creates("${root}/results/QJE_Figures_BlackAndWhite/Figure_3.svg")
	}

	yamlout using "${root}/results/paper numbers/`category'/Changes in Small Business Revenue vs `covar' (zip).yaml", ///
		key("bus_`key'_slope") ///
		comment("Slope (%`unit')") ///
		value(`display_beta') fmt(%9.2f)
	yamlout using "${root}/results/paper numbers/`category'/Changes in Small Business Revenue vs `covar' (zip).yaml", ///
		key("bus_`key'_se") ///
		comment("SE") ///
		value(`display_std_err') fmt(%9.2f)


	* Change in top 5% vs. bottom 5% of variable, for paper
	if "`var'" == "medhhinc_2018" local loc_name "income"
	else if "`var'" == "l_pop_conc_zcta" local loc_name "popdensity"
	else if "`var'" == "med_2br_2018" local loc_name "rent"

	gquantiles `loc_name'_ventile = `var' [w = pop_2018] if !mi(revenue_change_100), xtile nq(20)

	sum revenue_change_100 [w = pop_2018] if `loc_name'_ventile == 1
	local revenue_apr_`loc_name'_p5: di %2.0f abs(r(mean))
	sum revenue_change_100 [w = pop_2018] if `loc_name'_ventile == 20
	local revenue_apr_`loc_name'_p100: di %2.0f abs(r(mean))

	yamlout using "${root}/results/paper numbers/`category'/Changes in Small Business Revenue vs `covar' (zip).yaml", ///
		key("revenue_apr_`loc_name'_p5") ///
		comment("Revenue change in Apr 2020, first `loc_name' ventile") ///
		value(`revenue_apr_`loc_name'_p5') fmt(%2.0f)

	yamlout using "${root}/results/paper numbers/`category'/Changes in Small Business Revenue vs `covar' (zip).yaml", ///
		key("revenue_apr_`loc_name'_p100") ///
		comment("Revenue change in Apr 2020, last `loc_name' ventile") ///
		value(`revenue_apr_`loc_name'_p100') fmt(%2.0f)

	project, creates("${root}/results/paper numbers/`category'/Changes in Small Business Revenue vs `covar' (zip).yaml")
}
